﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using SFrost;
using ReportMobile.inc;

namespace ReportMobile {
    public partial class InCustom : System.Web.UI.Page {
        public DateTime rptDate;
        public string uId = "";
        public string msg = "";
        public StringBuilder sbCompany = new StringBuilder();  //查询SQL用，公司
        public SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnName"].ToString());
        public SqlCommand cmd = new SqlCommand();
        public DataSet dsRptGroup = new DataSet();
        public DataTable dtRptGroup;
        public DataRow drRptGroup;
        public DataSet dsDetail = new DataSet();
        public DataTable dtDetail;
        public DataRow drDetail;
        //2015-09-15：准备分公司的定额表，及相关取数，前端循环展现（只差定额）
        public DataSet dsQuota = new DataSet();
        public DataTable dtQuota;
        public DataRow drQuota;
        //明细模板开始
        public string tmpPl = "", tmpLx = "";
        public DataSet dsQuotaDetail = new DataSet();
        public DataTable dtQuotaDetail;
        public DataRow drQuotaDetail;
        protected void Page_Load(object sender, EventArgs e) {
            rptDate = DateTime.Now;
            if (Request.QueryString["uid"] == null || Request.QueryString["uid"].Trim() == "") {
                return;
            }
            else {
                uId = Request.QueryString["uid"].ToString();
            }
            Frost f = new Frost();
            cmd.Connection = conn;
            conn.Open();
            StringBuilder sb = new StringBuilder("select FId, FOAId, FVariety, FRegDate,funitid,funitcustom from TAuth where foaid = '" + uId + "'");
            if (f.selectCount(sb) < 1) {
                msg = "未查找到用户：" + uId + " 的授权，请与系统管理员联系。";
                return;
            }
            dtRptGroup = f.getDataTable(sb);
            drRptGroup = dtRptGroup.Rows[0];
            if (drRptGroup["funitid"] != null) {
                //确认是否有【自定义】查看权限
                if (drRptGroup["funitid"].ToString().IndexOf('4') < 0 || drRptGroup["funitcustom"].ToString().Length < 5) {
                    msg = "未找到【分公司】相关的查询权限，请联系管理员。";
                    return;
                }
            }
            //【非川渝区】不存在品类权限
            //加工成sql where公司
            string[] authCompany = drRptGroup["funitcustom"].ToString().Split(',');
            for (int i = 0; i < authCompany.Length; i++) {
                sbCompany.Append(",'" + authCompany[i].Trim().Replace(" ", "") + "'");
            }
            sbCompany.Remove(0, 1);

            //标准统计开始
            //dtRptGroup.Clear();  //此处如果不清理，点击到期未还后，跳转到本页面，该变量仍然有数，前台就会报错
            Common c = new Common();
            string userAgent = Request.UserAgent;
            if (Request.QueryString["upl"] == null || Request.QueryString["upl"].Trim().Length < 1) {
                c.SF_UserLog(Request.QueryString["uid"], "分公司", DateTime.Now,userAgent);
                StringBuilder sbRpt = new StringBuilder("select 公司简称,SUM(欠款余额) 欠款余额 from View_ES_客户借贷款明细");
                sbRpt.AppendFormat(" where 制表单位 in ({0})", sbCompany.ToString());
                sbRpt.Append(" group by 公司简称");
                cmd.CommandText = sbRpt.ToString();
                try {
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dsRptGroup);
                    dtRptGroup = dsRptGroup.Tables[0];
                    //清理后再用
                    cmd.Dispose();
                    sbRpt.Clear();
                    sbRpt.Append("select 制表单位,公司简称,借款类型,SUM(欠款余额) 欠款余额 from View_ES_客户借贷款明细");
                    sbRpt.AppendFormat(" where 制表单位 in ({0})", sbCompany.ToString());
                    sbRpt.Append(" group by 制表单位,公司简称,借款类型");
                    cmd.CommandText = sbRpt.ToString();
                    sda.Fill(dsDetail);
                    dtDetail = dsDetail.Tables[0];
                    //清理后再用，取定额
                    cmd.Dispose();
                    sbRpt.Clear();
                    sbRpt.Append("SELECT 制表人,年度,公司简称,公司名称,借款定额,贷款定额,合计定额 from View_ES_非川渝公司定额");
                    cmd.CommandText = sbRpt.ToString();
                    sda.Fill(dsQuota);
                    dtQuota = dsQuota.Tables[0];

                }
                catch (Exception sfe) {
                    msg = sfe.Message;
                    return;
                }
                conn.Close();
                f.close();
            }
            else {
                c.SF_UserLog(Request.QueryString["uid"], "分公司明细", DateTime.Now,userAgent);
                tmpPl = Request.QueryString["upl"].ToString();
                tmpLx = Request.QueryString["ulx"].ToString();
            }
        }
    }
}